<template>
  <div class="teacher">
    <div class="teacher-header">
      <div class="title">{{ datas.title }}</div>
      <div class="desc">{{ datas.desc }}</div>
    </div>
    <div class="teacher-body">
      <el-form :model="datas.form" size="small" label-width="82px">
        <!-- 板块名称选择 -->
        <el-form-item label="标题名称">
          <el-input
            v-model="datas.form.title"
            type="text"
            placeholder="请输入标题名称"
            size="small"
            maxlength="16"
            show-word-limit
          ></el-input>
        </el-form-item>

        <!-- 排列方式选择 -->
        <el-form-item label="展示方式">
          <el-radio-group v-model="datas.form.arrange_type">
            <el-radio
              v-for="item in arrangeOptions"
              :key="item.label"
              :label="item.label"
            >
              {{ item.title }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 课程列数选择 -->
        <el-form-item v-if="datas.form.arrange_type == 1" label="列表样式">
          <el-radio-group v-model="datas.form.column">
            <el-radio
              v-for="item in columnOptions"
              :key="item.label"
              :label="item.label"
            >
              {{ item.title }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 课程数量选择 -->
        <el-form-item label="展示讲师数">
          <el-input-number
            v-model="datas.form.number"
            :min="1"
            :max="99"
            @change="handlerChangeNumber"
          ></el-input-number>
        </el-form-item>

        <!-- 数据来源选择 -->
        <el-form-item label="讲师来源">
          <el-radio-group v-model="datas.form.source" @change="handlerChangeSource">
            <el-radio
              v-for="item in options"
              :key="item.label"
              :label="item.label"
            >
              {{ item.title }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 自定义讲师-->
        <el-form-item v-if="datas.form.source === 0" label="自定义讲师">
          <el-button type="primary" size="mini" @click="chooseTeacherVisible = true">选择讲师</el-button>
        </el-form-item>
      </el-form>
      <div v-if="datas.form.source ===0" class="teacher-footer">
        <div class="title">已选讲师 {{ datas.form.source_list.length }}/{{ datas.form.number||0 }} </div>
        <div v-show="datas.form.source_list.length > 0" class="box">
          <div v-for="item in datas.form.source_list" :key="item.value" class="item">
            <span class="text">{{ item.title }}</span>
            <i class="el-icon-close close" @click="removeSource(item.value)"></i>
          </div>
        </div>
      </div>
    </div>
    <!-- 选择讲师 start-->
    <choose-teacher-dialog
      v-if="chooseTeacherVisible"
      :visible.sync="chooseTeacherVisible"
      :limit="datas.form.number"
      :list="datas.form.source_list"
      @submit="setCurrentTeacher"
    ></choose-teacher-dialog>
    <!-- 选择讲师 end-->
  </div>
</template>

<script>
import ChooseTeacherDialog from '@/pages/user/teacher/components/ChooseTeacherDialog'

export default {
  name: 'TeacherStyle',
  components: {
    ChooseTeacherDialog
  },
  props: {
    datas: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      arrangeOptions: [
        {
          label: 1,
          title: '宫格'
        },
        {
          label: 2,
          title: '滚动'
        }
      ],
      columnOptions: [
        {
          label: 2,
          title: '2列'
        },
        {
          label: 3,
          title: '3列'
        },
        {
          label: 4,
          title: '4列'
        }
      ],
      options: [
        {
          label: 1,
          title: '默认'
        },
        {
          label: 0,
          title: '自定义'
        }
      ],
      chooseTeacherVisible: false
    }
  },
  methods: {
    // 设置当前选中的讲师
    setCurrentTeacher(value, teacher) {
      this.datas.form.source_list = teacher
      this.chooseTeacherVisible = false
      this.setSourceIndex()
    },
    // 移除选中的资源
    removeSource(val) {
      const index = this.datas.form.source_list.findIndex(item => item.value === val)
      this.datas.form.source_list.splice(index, 1)
      this.setSourceIndex()
    },
    // 数量改变
    handlerChangeNumber() {
      this.setSourceIndex()
    },
    // 数据来源改变
    handlerChangeSource(val) {
      this.setSourceIndex()
    },
    setSourceIndex() {
      if (this.$util.isUndef(this.datas.form.source_index)) {
        this.$set(this.datas.form, 'source_index', 0)
      }
      this.datas.form.source_index = Math.random()
    }
  }
}
</script>

<style scoped lang="scss">
.teacher {
  padding-top: 20px;
  padding-left: 24px;
  padding-right: 34px;
  &-header {
    padding-bottom: 10px;
    border-bottom: 1px solid #ebeef5;
    .title {
      font-size: 16px;
      font-weight: 500;
      color: #303133;
      line-height: 24px;
    }
    .desc {
      margin-top: 10px;
      font-size: 12px;
      font-weight: 400;
      color: #909399;
      line-height: 16px;
    }
  }
  &-body {
    margin-top: 20px;
    ::v-deep.el-radio {
      margin-right: 20px;
      &:last-child {
        margin-right: 0;
      }
    }
  }

  &-footer {
    margin-top: 20px;
    .title {
      font-size: 12px;
      font-weight: 400;
      color: #b7bac1;
      line-height: 16px;
    }
    .box {
      margin-top: 10px;
      background: #f7f7f7;
      padding: 0 10px;
    }
    .item {
      padding: 12px 0;
      display: flex;
      align-items: center;
      & + .item {
        border-top: 1px solid #ebeef5;
      }
      .text {
        flex: 1;
        min-width: 0;
        font-size: 14px;
        font-weight: 400;
        color: #303133;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .close {
        font-size: 12px;
        color: #8c8c8c;
        cursor: pointer;
      }
    }
  }
}
</style>
